﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using Eventhere.Work;
using Eventhere;
using System.IO.IsolatedStorage;
using System.Windows.Media.Animation;

namespace Eventhere.Repert
{
    public partial class Repert : PhoneApplicationPage
    {

        Repertory rep;
       
        public Repert()
        {
            
            InitializeComponent();
            this.OptionPane.Cancel += OptionPane_Cancel;
            this.OptionPane.AddUser += OptionPane_AddUser;
            this.OptionPane.EditUser += OptionPane_EditUser;
            this.OptionPane.RemoveUser += OptionPane_RemoveUser;
            this.OptionPane.AddGroupe += OptionPane_AddGroupe;
            this.ListRepertory.clickcontact += VoirUser;
            this.ListRepertory.clickgroupcontact += Unbutton_Click;


            if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
                this.rep = (Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"];


        }

        void OptionPane_AddGroupe(object arg1, EventArgs arg2)
        {
            NavigationService.Navigate(new Uri("/AddGroup.xaml", UriKind.RelativeOrAbsolute));   
        }

        private void VoirUser(object sender, EventArgs e)
        {
            int id = (int)((Button)sender).Tag;
            NavigationService.Navigate(new Uri("/SeeUser.xaml?idC=" + id.ToString() + "", UriKind.Relative));   
            
        }

        void OptionPane_RemoveUser(object arg1, EventArgs arg2)
        {
            
        }

        void OptionPane_EditUser(object arg1, EventArgs arg2)
        {
            foreach (Button b in this.ListRepertory.ContentRepertoryPanel.Children)
            {
                b.Click -= VoirUser;
                b.Click += b_Click;
                
            }
            MessageBox.Show("Please select the user");
        }

        void b_Click(object sender, EventArgs e)
        {
            ((Button)sender).Background = new SolidColorBrush(Colors.Orange);
            NavigationService.Navigate(new Uri("/EditContact.xaml", UriKind.RelativeOrAbsolute));
            ((Button)sender).Click -= b_Click;
            ((Button)sender).Click += VoirUser;
        }




        void OptionPane_AddUser(object arg1, EventArgs arg2)
        {
            NavigationService.Navigate(new Uri("/AddContact.xaml", UriKind.RelativeOrAbsolute));
        }

        void OptionPane_Cancel(object arg1, EventArgs arg2)
        {
            NavigationService.Navigate(new Uri("/Home/Home.xaml", UriKind.RelativeOrAbsolute));
        }


        void Unbutton_Click(object sender, EventArgs e)
        {
            int id = int.Parse(((Button)sender).Name);
            IsolatedStorageSettings.ApplicationSettings["GroupId"] = (Repertory.GetGroupById(id));
            NavigationService.Navigate(new Uri("/Repert/RepertoryModel.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}